package graphiceditor4robot.more.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import fsm.diagram.part.FsmCreationWizardPage;
import fsm.diagram.part.FsmDiagramEditorPlugin;
import fsm.diagram.part.FsmDiagramEditorUtil;
import fsm.diagram.part.Messages;

public class FsmCreationFromBehaviourWizard extends Wizard {

	private FsmCreationWizardPage domainModelFilePage;
	
	private String behaviourName;
	
	protected Resource diagram;

	private IStructuredSelection selection;
	
	public void init(IWorkbench workbench, String behaviourName, IStructuredSelection selection) {
		this.behaviourName = behaviourName;
		this.selection = selection;
		setWindowTitle(Messages.FsmCreationWizardTitle);
		setDefaultPageImageDescriptor(FsmDiagramEditorPlugin
				.getBundledImageDescriptor("icons/wizban/NewFsmWizard.gif")); //$NON-NLS-1$
		setNeedsProgressMonitor(true);
	}
	
	public void addPages() {
		domainModelFilePage = new FsmCreationWizardPage(
				"DomainModelFile", selection, "fsm.xmi");
		domainModelFilePage
		.setTitle(Messages.FsmCreationWizard_DomainModelFilePageTitle);
		domainModelFilePage
				.setDescription(Messages.FsmCreationWizard_DomainModelFilePageDescription);
		addPage(domainModelFilePage);
		domainModelFilePage.setFileName(behaviourName);
		
	}
	
	@Override
	public boolean performFinish() {
		IRunnableWithProgress op = new WorkspaceModifyOperation(null) {

			protected void execute(IProgressMonitor monitor)
					throws CoreException, InterruptedException {
				diagram = FsmDiagramEditorUtil.createDiagram(
						generateDomainURI(),
						domainModelFilePage.getURI(), monitor);
				if (diagram != null) {
					try {
						FsmDiagramEditorUtil.openDiagram(diagram);
					} catch (PartInitException e) {
						ErrorDialog.openError(getContainer().getShell(),
								Messages.FsmCreationWizardOpenEditorError,
								null, e.getStatus());
					}
				}
			}
		};
		try {
			getContainer().run(false, true, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			if (e.getTargetException() instanceof CoreException) {
				ErrorDialog.openError(getContainer().getShell(),
						Messages.FsmCreationWizardCreationError, null,
						((CoreException) e.getTargetException()).getStatus());
			} else {
				FsmDiagramEditorPlugin.getInstance().logError(
						"Error creating diagram", e.getTargetException()); //$NON-NLS-1$
			}
			return false;
		}
		return diagram != null;
	}
	
	private URI generateDomainURI() {
		// double extension
		return domainModelFilePage.getURI().trimFileExtension().trimFileExtension().appendFileExtension("fsm_diag");
	}

}
